package org.rsbot.script.randoms;

import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.GroundItems;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Settings;
import org.rsbot.script.methods.Tiles;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSGroundItem;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Arbiter"}, name="ScapeRuneIsland", version=2.2D)
public class ScapeRuneIsland extends Random
{
  public int[] STATUE_IDS = { 8992, 8993, 8990, 8991 };
  public RSTile CENTER_TILE = new RSTile(3421, 4777);
  public RSObject direction;
  public boolean finished;
  public boolean fishing;
  public boolean forceTalk;

  public boolean activateCondition()
  {
    return this.calc.distanceTo(this.CENTER_TILE) < 50;
  }

  public void onFinish() {
    this.direction = null;
    this.finished = false;
    this.fishing = false;
  }

  public int loop() {
    if (!activateCondition()) {
      return -1;
    }
    RSObject localRSObject1 = this.objects.getNearest(new int[] { this.STATUE_IDS[0] });
    RSObject localRSObject2 = this.objects.getNearest(new int[] { this.STATUE_IDS[1] });
    RSObject localRSObject3 = this.objects.getNearest(new int[] { this.STATUE_IDS[2] });
    RSObject localRSObject4 = this.objects.getNearest(new int[] { this.STATUE_IDS[3] });
    if ((getMyPlayer().isMoving()) || (getMyPlayer().getAnimation() == 620)) {
      return random(550, 700);
    }
    if ((this.interfaces.get(241).getComponent(4).isValid()) && (this.interfaces.get(241).getComponent(4).getText().contains("catnap")))
      this.finished = true;
    if ((this.interfaces.get(64).getComponent(4).isValid()) && (this.interfaces.get(64).getComponent(4).getText().contains("fallen asleep")))
      this.finished = true;
    if ((this.interfaces.get(242).getComponent(4).isValid()) && (this.interfaces.get(242).getComponent(4).getText().contains("Wait! Before")))
      this.forceTalk = true;
    if ((this.interfaces.canContinue()) && 
      (this.interfaces.clickContinue()))
      return random(500, 1000);
    if (this.forceTalk) {
      localObject = this.npcs.getNearest(new int[] { 2481 });
      if ((localObject != null) && (this.direction == null) && (this.settings.getSetting(344) == 0)) {
        if (!this.calc.tileOnScreen(((RSNPC)localObject).getLocation())) {
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(((RSNPC)localObject).getLocation()));
          return 700;
        }
        if (((RSNPC)localObject).doAction("Talk-to"))
          this.forceTalk = false;
        return random(1000, 2000);
      }
      if (localObject == null) {
        localObject = this.npcs.getNearest(new int[] { 2481 });
        if (localObject == null) {
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.CENTER_TILE));
          return random(1000, 2000);
        }
        return random(50, 100);
      }
    }
    if (this.finished) {
      localObject = this.objects.getNearest(new int[] { 8987 });
      if (localObject != null) {
        if (!this.calc.tileOnScreen(((RSObject)localObject).getLocation())) {
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(((RSObject)localObject).getLocation()));
          return random(500, 1000);
        }
        if (((RSObject)localObject).doAction("Enter"))
          return random(3000, 5000);
        return random(500, 1000);
      }

      this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.CENTER_TILE));
    }

    if (this.bank.isDepositOpen()) if (this.bank.getBoxCount() - this.bank.getBoxCount(new int[] { 6209, 6202, 6200 }) >= 27) {
        localObject = this.interfaces.get(11).getComponent(17).getComponent(random(16, 26));
        int i = ((RSComponent)localObject).getComponentID();
        if (i < 0)
          return random(50, 100);
        log("Item with ID " + i + " was deposited.");
        if (this.interfaces.get(11).getComponent(17).getComponent(random(16, 26)).doAction("Dep"))
          return random(500, 1000);
        return random(50, 100);
      }
    if (this.bank.isDepositOpen()) if (this.bank.getBoxCount() - this.bank.getBoxCount(new int[] { 6209, 6202, 6200 }) < 27) {
        this.bank.close();
        return random(500, 1000);
      }
    if (this.inventory.getCountExcept(new int[] { 6209, 6202, 6200 }) >= 27) {
      localObject = this.objects.getNearest(new int[] { 32930 });
      if (!this.calc.tileOnScreen(((RSObject)localObject).getLocation())) {
        this.walking.walkTileMM(this.walking.getClosestTileOnMap(((RSObject)localObject).getLocation()));
        return random(1000, 2000);
      }
      log("Depositing item(s) to make room.");
      ((RSObject)localObject).doAction("Deposit");
      return random(500, 1000);
    }

    if (this.inventory.getCount(new int[] { 6202 }) > 0) {
      localObject = this.objects.getNearest(new int[] { 8985 });
      if (localObject != null) {
        if (!this.calc.tileOnScreen(((RSObject)localObject).getLocation())) {
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(((RSObject)localObject).getLocation()));
          return random(400, 800);
        }
        this.inventory.getItem(new int[] { 6202 }).doAction("Use");
        sleep(random(800, 1000));
        if (((RSObject)localObject).doAction("Use")) {
          sleep(1000);
        }
        return random(2000, 2400);
      }
      this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.CENTER_TILE));
    }

    if (this.fishing) if (this.inventory.getCount(new int[] { 6209 }) == 0) {
        localObject = this.groundItems.getNearest(new int[] { 6209 });
        if (localObject != null) {
          if (!this.calc.tileOnScreen(((RSGroundItem)localObject).getLocation())) {
            this.walking.walkTileMM(this.walking.getClosestTileOnMap(((RSGroundItem)localObject).getLocation()));
            return random(800, 1000);
          }
          this.tiles.doAction(((RSGroundItem)localObject).getLocation(), "Take");
          return random(800, 1000);
        }

        this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.CENTER_TILE));
      }

    if ((this.interfaces.get(246).getComponent(5).containsText("contains")) && (this.settings.getSetting(334) == 1) && (this.direction == null))
    {
      sleep(2000);
      if (this.calc.tileOnScreen(localRSObject1.getLocation())) {
        this.direction = localRSObject1;
        this.fishing = true;
      }
      if (this.calc.tileOnScreen(localRSObject2.getLocation())) {
        this.direction = localRSObject2;
        this.fishing = true;
      }
      if (this.calc.tileOnScreen(localRSObject3.getLocation())) {
        this.direction = localRSObject3;
        this.fishing = true;
      }
      if (this.calc.tileOnScreen(localRSObject4.getLocation())) {
        this.direction = localRSObject4;
        this.fishing = true;
      }
      log("Checking direction");
      return random(2000, 3000);
    }
    if (this.direction != null) if (this.inventory.getCount(new int[] { 6200 }) < 1) {
        sleep(random(1000, 1200));
        if (!this.calc.tileOnScreen(this.direction.getLocation())) {
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.direction.getLocation()));
          return random(400, 600);
        }
        localObject = this.objects.getNearest(new int[] { 8986 });
        if (localObject != null) {
          if (!this.calc.tileOnScreen(((RSObject)localObject).getLocation())) {
            this.camera.turnToTile(((RSObject)localObject).getLocation());
          }
          if ((!this.calc.tileOnScreen(((RSObject)localObject).getLocation())) && (this.walking.walkTileMM(((RSObject)localObject).getLocation())))
          {
            sleep(random(1000, 2000));
            if (!this.calc.tileOnScreen(((RSObject)localObject).getLocation())) {
              sleep(1000);
            }
          }
          this.tiles.doAction(((RSObject)localObject).getLocation(), "Net");
          return random(2000, 2500);
        }
        this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.CENTER_TILE));
      }

    if (this.inventory.getCount(new int[] { 6200 }) > 0) {
      localObject = this.npcs.getNearest(new int[] { 2479 });
      if (localObject != null) {
        if (!this.calc.tileOnScreen(((RSNPC)localObject).getLocation())) {
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(((RSNPC)localObject).getLocation()));
        }
        this.inventory.getItem(new int[] { 6200 }).doAction("Use");
        sleep(random(500, 1000));
        ((RSNPC)localObject).doAction("Use Raw fish-like thing -> Evil bob");
      } else {
        this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.CENTER_TILE));
      }
      return random(1900, 2200);
    }
    Object localObject = this.npcs.getNearest(new int[] { 2481 });
    if ((localObject != null) && (this.direction == null) && (this.settings.getSetting(344) == 0)) {
      if (!this.calc.tileOnScreen(((RSNPC)localObject).getLocation())) {
        this.walking.walkTileMM(this.walking.getClosestTileOnMap(((RSNPC)localObject).getLocation()));
        return 700;
      }
      ((RSNPC)localObject).doAction("Talk-to");
      return random(1000, 2000);
    }
    if (localObject == null) {
      localObject = this.npcs.getNearest(new int[] { 2481 });
      if (localObject == null) {
        this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.CENTER_TILE));
        return random(1000, 2000);
      }
      return random(50, 100);
    }
    log("Setting 344: " + this.settings.getSetting(344));
    return random(800, 1200);
  }
}